package juc.c_021_01Interview;
/*
   Talk is cheap. Show me the code.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @Description
 * @Date 2021/6/20
 * @Author liudl
 */
public class T06_LockSupport {
    volatile List lists = new ArrayList();

    public boolean add(Object o) {
        return lists.add(o);
    }

    public int size() {
        return lists.size();
    }

    static Thread t1, t2;

    public static void main(String[] args) {
        T06_LockSupport t = new T06_LockSupport();
        t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " start");
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                t.add(i);
                if (t.size() == 5) {
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
            System.out.println(Thread.currentThread().getName() + " end");
        }, "t1");
        t2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " start");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + " end");
            LockSupport.unpark(t1);
        }, "t2");
        t2.start();
        t1.start();
    }
}
